package com.rui.controller;

import com.rui.entity.Student;
import com.rui.entity.Subject;
import com.rui.service.SubjectService;
import com.sun.deploy.net.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2017/4/21.
 */
@Controller
@RequestMapping("/subject")
public class SubjectController {

    @Autowired
    private SubjectService subjectService;

    @RequestMapping("/add")
    public String add(Subject subject){
        subjectService.add(subject);
        return "subject/list";
    }

    @RequestMapping("/check")
    public boolean check(String name,Integer id){
        List<Subject> subjects = subjectService.findSubjects(name,id);
        boolean pass;
        if(subjects.size()>0){
            pass = false;
        }else{
            pass = true;
        }
        return pass;
    }

    @RequestMapping("/delete")
    public String delete(Integer id){
        Subject subject = subjectService.findOne(id);
        subjectService.delete(subject.getId());
        return "subject/list";
    }

    @RequestMapping("/detail")
    public String detail(Integer id){
        Subject subject = subjectService.findOne(id);
        return "subject/edit";
    }

    @RequestMapping("/list")
    public String index(String name,Integer pageSize,Integer pageIndex){
        List<Subject> subjects = subjectService.findSubjects(name,pageSize,pageIndex);
        Integer totalPage = subjectService.findTotalPage(name,pageSize);
        return "subject/list";
    }

    @RequestMapping("/select")
    public String subjects(String subName,Integer subId,HttpSession session){
        Student student = (Student) session.getAttribute("keyTwo");
        List<Subject> subjects = subjectService.findSubjects(subName,subId);
        return "studentSubject/select";
    }

    @RequestMapping("/update")
    public String update(Subject subject){
        subjectService.update(subject);
        return "subject/list";
    }

    @RequestMapping("/toAdd")
    public String toAdd(){
        return "subject/add";
    }
}
